import { Body, Controller, Post, Inject } from '@midwayjs/core';
import { ApiResponse, ApiTags } from '@midwayjs/swagger';
import {
  UserLoginResDto,
  UserLoginReq,
  PhoneDto,
  UserCodeResDto,
  AdminLoginReq,
  UpdateUserReq,
  FlagResDto,
  UserDetailsDto,
  Pagination,
  UserItemResDto,
} from '../model';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service';
import { ServerResponse } from '../util/response';

@ApiTags(['用户'])
@Controller('/user')
export class UserController {
  @Inject()
  ctx: Context;

  @Inject()
  userService: UserService;

  @Post('/login', { summary: '用户登录' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: UserLoginResDto,
  })
  async login(@Body() req: UserLoginReq) {
    if (req.code !== 123456) {
      return ServerResponse.JSON_TPL({}, false, '验证码错误');
    }
    const result = await this.userService.createAccount(req.phone);
    const sign = JSON.stringify({
      id: result.id,
      phone: result.phone,
    });
    const token = await this.userService.getToken(sign);
    return ServerResponse.JSON_TPL({
      token,
    });
  }
  @Post('/code', { summary: '验证码' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: UserCodeResDto,
  })
  async code(@Body() req: PhoneDto) {
    if (req.phone.toString().length === 11) {
      return ServerResponse.JSON_TPL({
        code: 123456,
      });
    }
    return ServerResponse.JSON_TPL({}, false, '手机号错误');
  }

  @ApiResponse({
    status: 200,
    description: 'ok',
    type: UserLoginResDto,
  })
  @Post('/adminLogin', { summary: '后台登录' })
  async adminLogin(@Body() req: AdminLoginReq) {
    const result = await this.userService.adminLogin(req);
    if (!result) {
      return ServerResponse.JSON_TPL({}, false, '账号或密码错误');
    }
    const token = await this.userService.getAdminToken(result);
    return ServerResponse.JSON_TPL({
      token,
    });
  }

  @Post('/update', { summary: '更新用户' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async updateUser(@Body() req: UpdateUserReq) {
    const res = await this.userService.updateUser(this.ctx.state.user.id, req);
    if (Boolean(res[0])) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res[0]),
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: Boolean(res[0]),
      },
      Boolean(res[0]),
      '不存在该账号'
    );
  }

  @Post('/details', { summary: '获取用户详情' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: UserDetailsDto,
  })
  async userDetails() {
    const res = await this.userService.userDetails(this.ctx.state.user.id);
    return ServerResponse.JSON_TPL({
      data: res,
    });
  }

  @Post('/list', { summary: '获取用户列表' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: UserItemResDto,
  })
  async userList(@Body() req: Pagination) {
    const params: Pagination = {
      pageNo: 1,
      pageSize: 10,
      ...req,
    };
    const res = await this.userService.getList(params);
    return ServerResponse.JSON_TPL({
      list: res.rows,
      pagination: {
        ...params,
        total: res.count,
      },
    });
  }
}
